package com.bw.gulimall.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.bw.gulimall.product.dao.CategoryDao;
import com.bw.gulimall.product.entity.CategoryEntity;
import com.bw.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void addtree(CategoryEntity categoryEntity) {
        //接入参数有 name 一级下拉名字  name2 添加的二级下拉名  showstatus 状态
        //1.先通过一级下拉名字查询是否有1级下拉
        final CategoryEntity list = categoryDao.selectOne(new QueryWrapper<CategoryEntity>().eq("name", categoryEntity.getName()).eq("parent_cid", 0));
        //查询是否有这个一级下拉
        if (list!=null){
            CategoryEntity categoryEntity1 = new CategoryEntity();
            categoryEntity1.setName(categoryEntity.getName2());
            categoryEntity1.setParentCid(list.getCatId());
            categoryDao.insert(categoryEntity1);
        }else{
            //如果没有的话，添加一个一级分类再次添加下级
            CategoryEntity categoryEntity1 = new CategoryEntity();
            categoryEntity1.setName(categoryEntity.getName());
            categoryEntity1.setParentCid(0l);
            categoryEntity1.setShowStatus(1);
            categoryDao.insert(categoryEntity1);
            //添加下级分类。
            CategoryEntity categoryEntity2 = new CategoryEntity();
            categoryEntity2.setParentCid(categoryEntity1.getCatId());
            categoryEntity2.setName(categoryEntity.getName2());
            categoryEntity2.setShowStatus(categoryEntity.getShowStatus());
            categoryDao.insert(categoryEntity2);
    }
    }

}